﻿using Microsoft.Extensions.Logging;
using Simple.Framework.Configuration;
using Simple.Framework.Ioc;
using Simple.Framework.Load;
using Simple.Framework.Logger;
using Simple.Framework.Schedule;
using Simple.Tools;
using SimpleNet.Client;
using SimpleNet.Handle;
using SimpleNet.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 SimpleNet.Net
 * 接口名称 SimpleBuilder
 * 开发人员：ADMIN
 * 创建时间：2023/12/29 10:31:12
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace SimpleNet.Net
{
	public class SimpleBuilder: ISimpleBuilder
	{
		IContainer container;
		IConfiguration configution;
		ILogger<Host> logger;
		IClassLoader classLader;

		private SystemConfiguration systeConfig;
		List<IPretty> prettyCores { get; set; }

		public static ISimpleBuilder BuilderHost()
		{
			return new SimpleBuilder();
		}

		private SimpleBuilder()
		{
			container = Container.GetContainer();
			Initalization();
		}

		private void Initalization()
		{
			container.RegistSingletonInstance<ILoggerProvider, ClassNameLoggerProvider>(new ClassNameLoggerProvider(string.Empty));
			//ILoggerProvider provider = container.GetService<ILoggerProvider>();
			//ILogger logger=provider.CreateLogger("test");
			logger = container.GetService<ILogger<Host>>();

			container.Register<IContext, Context>().Autowird();
			container.Register<ISimpleJobject, SimpleJObject>().Autowird();

			container.RegisterSingleton<IConfiguration, Configuration>();

			configution = container.GetService<IConfiguration>();


			var systeConfig = SystemConfiguration.Builder();
			container.RegisterSingleton(systeConfig);

			#region  socket 消息处理
			container.RegisterSingleton<IMessageHandle, MessageHandle>().Autowird();
			container.RegisterSingleton<IMessageHandle, StringMessageHandle>().Autowird();
			if (ToolSimpleCheck.IsNull(systeConfig.MessageHandle))
			{
				systeConfig.MessageHandle = container.GetService<IMessageHandle>();
			}

			#region 任务管理
			container.RegisterSingleton<IScheduleManager, ScheduleManager>().Autowird();
			#endregion

			#region
			container.Register<IScheduleHandle, ScheduleHandle>().Autowird();
			#endregion

			container.Register<IConnection, Connection>().Autowird();
			container.RegisterSingleton<IClassLoader, ClassLoader>().Autowird();
			classLader = container.GetService<IClassLoader>();

			RegisterClassLoadProcess<LoadClassLoadProcess>();

	

			container.RegisterSingleton<IMDTDataPackProtocol, MDTDataPackProtocol>();


			//logger.LogInformation("容器初始化成功!");
#if DEBUG
			logger.LogInformation("Initalization successfully!");
#endif
		}

		private void ServiceConfiguration()
		{
			#region socket 客户端管理
			container.RegisterSingleton<IClientManager, ClientManager>().Autowird();
			#endregion

			#region socket 监听
			container.RegisterSingleton<IPretty, Pretty>().Autowird();
			#endregion
		}


		private void ClientConfiguration()
		{
			container.Register<ISimpleClient,SimpleClient>().Autowird();
		}

		public void RegisterClassLoadProcess<TIClassLoadProcess>() where TIClassLoadProcess : IClassLoadProcess
		{
			container.Register<TIClassLoadProcess>().Autowird();
			var load = container.GetService<TIClassLoadProcess>();
			classLader.Register(load);
		}

		public ISimpleBuilder Regster(Action<IContainer> action)
		{
			if (action == null)
			{
				throw new ArgumentNullException(nameof(action));
			}
			action.Invoke(container);
			return this;
		}

		public ISimpleBuilder StartPrettyServer()
		{
			ServiceConfiguration();
			

			classLader.LoadClass();

		
			#endregion
			prettyCores = container.GetServiceAll<IPretty>().ToList();
			foreach (var prettyCore in prettyCores)
			{
				prettyCore.Start();
			}
			var scheduleManager = container.GetService<IScheduleManager>();
			scheduleManager.Start();
			return this;
		}



		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ConfigurationPath"></param>
		/// <returns></returns>
		public ISimpleBuilder SetConfiguration(string ConfigurationPath)
		{
			Configuration.SetConfiguration(ConfigurationPath);
			return this;

		}


		public ISimpleBuilder ConfigutionMessageHandle<TMessageHandle>() where TMessageHandle : IMessageHandle
		{
			var configuration = container.GetService<SystemConfiguration>();
			configuration.MessageHandle = container.GetService<TMessageHandle>();
			if (configuration.MessageHandle==null)
			{
				container.RegisterSingleton<IMessageHandle, TMessageHandle>().Autowird();
			}
			configuration.MessageHandle = container.GetService<TMessageHandle>();
			return this;
		}

		public ISimpleBuilder ConfigutionLogger(Action<ClassNameLoggerProvider> action)
		{
			if (action != null)
			{
				ClassNameLoggerProvider provider = (ClassNameLoggerProvider)container.GetService<ILoggerProvider>();
				action.Invoke(provider);
			}
			return this;
		}

		public T GetService<T>()
		{
			return container.GetService<T>();
		}

		public IContainer GetContainer()
		{
			return this.container;
		}
		public ISimpleClient CreateClient()
		{
			ClientConfiguration();
			ISimpleClient client =container.GetService<ISimpleClient>();
			return client;
		}

	}
}
